package route

import (
	"ecms/config"
	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	"time"
)

var Router *gin.Engine

func init() {
	if config.Gin().Debug {
		gin.SetMode(gin.DebugMode)
	} else {
		gin.SetMode(gin.ReleaseMode)
	}
	Router = gin.Default()
	Router.Use(cors.New(cors.Config{
		AllowAllOrigins: true,
		AllowMethods:    []string{"GET", "POST", "OPTIONS"},
		AllowHeaders:    []string{"Accept", "Authorization", "Content-Type", "Referer", "User-Agent", "Origin"},
		ExposeHeaders:   []string{"Token-Refresh"},
		MaxAge:          time.Hour * 12,
	}))
}

func SetRouter() {
	// thirdparty
	SetThirdParty(Router.Group("/thirdparty"))

	// admin
	SetAdmin(Router.Group("/admin"))

	// 静态文件服务
	Router.Use(func(ctx *gin.Context) { // 前端多任务截图依赖此跨域响应设置
		ctx.Writer.Header().Set("Content-Security-Policy", "sandbox allow-scripts; default-src 'none'; img-src 'self'; style-src 'self';")
		ctx.Writer.Header().Set("Access-Control-Allow-Origin", "*")
		ctx.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
		ctx.Writer.Header().Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS")
		ctx.Writer.Header().Set("Access-Control-Allow-Headers", "Accept,Authorization,Content-Type,Referer,User-Agent,Origin")
	})
	{
		Router.Static("/static", "./static")
	}
}
